package com.jcli.jb51_net.article._271803_Pong.com.mr.model;

import java.awt.*;
import java.util.Random;

/**
 * TODO 3.球
 *一般情况下都有x和y还有大小，因为这个是个圆，所以我们用半径r，
 * 然后xspeed和yspeed表示各个方向的速度从而实现斜着移动，
 * 还有max_speed和min_speed用于把动态变换的速度限制于这个范围内，
 * 待会每碰到一次墙壁或板，就会适当增加或减少速度，所以要把速度限制在特定范围内，max_speed2和min_speed2也一样，
 * 前2者是用于正数速度的，后2者是用于负数速度的，然后移动的时候就对一些碰撞等情况进行检测就好了，
 * getBound用于返回对象的Rect长方形对象，用于检测碰撞。
 * @author li
 * @version 1.0
 * @belongsProject novel_20230923
 * @belongsPackage com.jcli.jb51_net.article._271803_Pong.com.mr.model
 * @className Ball
 * @createTime 2024-02-24  08:38
 */
public class Ball {
    public int x;
    public int y;
    public int r;
    private int xspeed;
    private int yspeed;
    private int max_speed;
    private int min_speed;
    private int max_speed2;
    private int min_speed2;

    public Ball() {
        x = 425;
        y = 500;
        r = 15;
        Random rd = new Random();
        // xspeed = rd.nextInt(1, 3) == 1 ? rd.nextInt(4, 7) : rd.nextInt(-6, -3);随机数的格式不对
        xspeed = rd.nextInt(3) + 1 == 1 ? rd.nextInt(4) + 4 : rd.nextInt(4) - 6;
        xspeed = rd.nextInt(3) + 1 == 1 ? rd.nextInt(4) + 4 : rd.nextInt(4) - 6;
        max_speed = 12;
        min_speed = 3;
        max_speed2 = -3;
        min_speed2 = -12;
    }

    public void move(Rectangle b1, Rectangle b2) {
        x += xspeed;
        y += yspeed;
        Random rd = new Random();
        if (getBound().intersects(b1) || getBound().intersects(b2)) {
            xspeed = -xspeed;
            // yspeed += rd.nextInt(-2, 3);
            yspeed += rd.nextInt(6) - 2;
        } else if (y - r <= 0 || y + r >= 965) {
            yspeed = -yspeed;
            xspeed += rd.nextInt(6) - 2;
        }
        if (xspeed > max_speed && xspeed > 0) {
            xspeed = max_speed;
        }
        if (yspeed > max_speed && yspeed > 0) {
            yspeed = max_speed;
        }
        if (xspeed < min_speed && xspeed > 0) {
            xspeed = min_speed;
        }
        if (yspeed < min_speed && yspeed > 0) {
            yspeed = min_speed;
        }
        if (xspeed > max_speed2 && xspeed < 0) {
            xspeed = max_speed2;
        }
        if (yspeed > max_speed2 && yspeed < 0) {
            yspeed = max_speed2;
        }
        if (xspeed < min_speed2 && xspeed < 0) {
            xspeed = min_speed2;
        }
        if (yspeed < min_speed2 && yspeed < 0) {
            yspeed = min_speed2;
        }
    }

    private Rectangle getBound() {
        return new Rectangle(x - r, y - r, r * 2, r * 2);
    }
}
